/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.pvacameras.server.config;

import javax.servlet.http.HttpServletRequest;

/**
 * Static configuration class that handles constants.  I recommend not using
 * this method and storing your keys in a secure area.
 * 
 * This was implemented for convenience's sake.
 * 
 * @author pying(peng ying)
 *
 */
public class Config {

  /**
   * private default constructor
   */
  private Config(){}
  
  /**
   * Available environments
   * 
   * SANDBOX - Test environment where no real world transactions are processed
   * PRODUCTION - Production environment with real credit cards
   *
   */
  enum Environment {
    SANDBOX, PRODUCTION, DEV;
  }
  
  //Set the environment that you're deploying against
  private static Environment env = Environment.SANDBOX;
  
  public static final String MERCHANT_NAME = "PVA Cameras";
  //Credentials
  public static final String MERCHANT_ID = "15900245939975655206";
  public static final String MERCHANT_SECRET = "Uc8I7uYRU-5Lx_-HlxGIpg";
  
  public static final String PROD_MERCHANT_ID = "9855515614340869668";
  public static final String PROD_MERCHANT_SECRET = "g3pGB0MeNCeblEeVddxZlQ";
  
  public static final String OAUTH_CLIENT_ID = "765560632621-8pf9sqbicgb8citielj6tnh8n46i7i7s.apps.googleusercontent.com";
  public static final String OAUTH_API_KEY = "G039xWX_f6EuOKZ_SGzFTXyk";
  //Javascript source URL
  public static final String WALLET_JS_URL = 
      "https://wallet-web.sandbox.google.com/online/v2/merchant/merchant.js";
  public static final String PROD_WALLET_JS_URL = 
      "https://wallet.google.com/online/v2/merchant/merchant.js";
  //public static final String WALLET_JS_URL =
  //    "https://wallet-web.sandbox.google.com/dev/online/v2/merchant/merchant.js";
  /**
   * Helper function to check the current environment
   * 
   * @return boolean based on if the environment to run against is sandbox
   */
  public static boolean isSandbox(){
    return env.equals(Environment.SANDBOX);
  }
  
  /**
   * Helper function to get Merchant Id based on the configured environment
   * 
   * @return Merchant Id
   */
  public static String getMerchantId(){
    return isSandbox() ? MERCHANT_ID : PROD_MERCHANT_ID;
  }
  
  /**
   * Helper function to get Merchant Secret based on the configured environment
   * 
   * @return Merchant Secret
   */
  public static String getMerchantSecret(){
    return isSandbox() ? MERCHANT_SECRET : PROD_MERCHANT_SECRET;
  }
  
  /**
   * Helper function to get the Wallet JS URL based on environment
   * 
   * @return Wallet JS URL 
   */
  public static String getJsUrl(){
    return isSandbox() ? WALLET_JS_URL : PROD_WALLET_JS_URL;
  }
  
  //Request currency
  public static final String CURRENCY = "USD";
  
  /**
   * Helper function to return the protocol://domain:port 
   * 
   * @param req servlet request
   * @return request protocol://domain:port
   */
  public static String getDomain(HttpServletRequest req){
    String domain = req.getServerName();
    String protocol = req.getScheme();
    String port = Integer.toString(req.getServerPort());
    String origin = protocol + "://" + domain;
    if(!(port.equals("80") || port.equals("443"))){
      origin += ":" + port;
    }
    return origin;
  }
}
